package com.yxinmiracle.canal.listenter;

import com.alibaba.fastjson.JSON;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.xpand.starter.canal.annotation.*;
import com.yxinmiracle.content.feign.ContentFeign;
import com.yxinmiracle.content.pojo.Content;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;

/**
 * @version 1.0
 * @author： YxinMiracle
 * @date： 2021-08-11 16:35
 */
@CanalEventListener
public class MyEventListener {
//    @InsertListenPoint
//    public void onEvent(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
//        //do something...
//    }
//
//    @UpdateListenPoint
//    public void onEvent1(CanalEntry.RowData rowData) {
//        //do something...
//        List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
//        for (CanalEntry.Column column : beforeColumnsList) {
//            System.out.println(column.getName()+" : "+column.getValue());
//        }
//        System.out.println("===================更新之后==================");
//        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
//        for (CanalEntry.Column column : afterColumnsList) {
//            System.out.println(column.getName()+" : "+column.getValue());
//        }
//    }

//    @DeleteListenPoint
//    public void onEvent3(CanalEntry.EventType eventType) {
//        //do something...
//    }


    @Autowired
    private ContentFeign contentFeign;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * destination 是linux中的目录 也是目的地址
     * schema 数据库的库名
     * table 要监听的表名
     *
     * @param eventType
     * @param rowData
     */
    @ListenPoint(destination = "example",
            schema = "changgou_content",
            table = {"tb_content",
                    "tb_content_category"},
            eventType = {CanalEntry.EventType.UPDATE,
                    CanalEntry.EventType.DELETE,
                    CanalEntry.EventType.INSERT}
            )
    public void onEventCustomUpdate(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        // 判断
        String categoryId = getColumnValue(eventType, rowData);
        Result<List<Content>> result = contentFeign.findByCategory(Long.valueOf(categoryId));
        List<Content> data = result.getData();
        stringRedisTemplate.boundValueOps("content_"+categoryId).set(JSON.toJSONString(data));
    }

    /**
     * 获取categoryId
     * @param eventType
     * @param rowData
     * @return
     */
    private String getColumnValue(CanalEntry.EventType eventType, CanalEntry.RowData rowData){
        // 1. 判断如果是insert 和 update 那么就获取after的数据
        String categoryId = "";
        if(eventType== CanalEntry.EventType.DELETE){
            List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
            for (CanalEntry.Column column : beforeColumnsList) {
                if (column.getName().equals("category_id")) {
                    categoryId = column.getValue();
                    break;
                }
            }
        }else {
            List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
            for (CanalEntry.Column column : afterColumnsList) {
                if (column.getName().equals("category_id")) {
                    categoryId = column.getValue();
                    break;
                }
            }
        }
        return categoryId;
    }
}
